package com.market.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.Location;
import com.market.domain.Message;
import com.market.domain.Picture;
import com.market.domain.Role;
import com.market.domain.User;
import com.market.domain.transients.Email;
import com.market.manager.LocationManager;
import com.market.manager.MessageManager;
import com.market.util.CollectionUtil;
import com.market.web.model.LocationModel;
import com.market.web.model.MarketList;
import com.market.web.model.MessageModel;
import com.market.web.model.PictureModel;
import com.market.web.model.UserModel;

@Controller("messageController")
@Scope("session")
public class MessageController extends
		GenericController<MessageManager, MessageModel, Message> {

	@Autowired
	private LocationManager locationManager;

	@Autowired
	private EmailController emailController;

	@Autowired
	private UserModel userModel;
	@Autowired
	private LocationModel locationModel;
	@Autowired
	private PictureModel picModel;

	/*
	 * @Override public Message getNewObject() { Message message = new
	 * Message(); message.setIsNew(true); return message; }
	 */

	@Autowired
	@Override
	protected void setModel(MessageModel model) {
		this.model = model;
	}

	@Autowired
	@Override
	protected void setManager(MessageManager manager) {
		this.manager = manager;
	}

	public void initBlogs() {
		if (this.model.getIsInitialized() == false) {
			List<Message> all = manager.fetchAllBlogs();
			if (all.size() > 0) {
				model.setList(all);
				this.refreshCurrent();
			}
			this.model.setIsInitialized(true);
		}
	}

	/**
	 * This method send current message to all users that this message related
	 * to.
	 */
	public void sendCurrentToUsers() {
		for (Email email : this.getModelCurrent().getEmails()) {
			this.emailController.sendEmail(email);
		}
		super.popMessage("Done", "Send email to user.");
	}

	/**
	 * This method send current message to all users that belong to locations
	 * that this message related to.
	 */
	public void sendCurrentToLocations() {
		Message message = this.getModelCurrent();

		for (Location location : message.getLocations()) {
			List<User> users = this.locationManager.fetchUsers(location);
			for (User user : users) {
				if (user.getIsEnabled()) {//If this user was disabled, don't send email.
					Email email = message.getEmails(user);
					this.emailController.sendEmail(email);
				}
			}
		}

		super.popMessage("Done", "Send email to user(s) of location(s).");
	}

	/**
	 * Add all selected users to current message And clear selected propertied
	 * after this operation finish
	 * 
	 */
	public void addSelectedUsersToCurrent() {
		Message current = this.getModelCurrent();
		List<User> selectedUsers = userModel.getSelecteds();
		CollectionUtil<User> util = new CollectionUtil<User>();
		util.addAll(current.getUsers(), selectedUsers);
		util.clearSelection(selectedUsers); // Normally we have to clear
											// selected properties, after
											// operation finish
	}

	/**
	 * Add all selected Locations to current message And clear selected
	 * propertied after this operation finish
	 * 
	 */
	public void addSelectedLocationsToCurrent() {
		Message current = this.getModelCurrent();
		List<Location> selectedLocations = locationModel.getSelecteds();
		CollectionUtil<Location> util = new CollectionUtil<Location>();
		util.addAll(current.getLocations(), selectedLocations);
		util.clearSelection(selectedLocations); // Normally we have to clear
		// selected properties, after
		// operation finish
	}

	/**
	 * Remove all selected users from current message And clear selected
	 * propertied after this operation finish
	 * 
	 */

	public void removeSelectedUsersFromCurrent() {
		List<User> users = this.getModel().getCurrent().getUsers();
		CollectionUtil<User> util = new CollectionUtil<User>();
		List<User> unSelectedUsers = util.extractUnSelected(users);
		this.getModelCurrent().setUsers(unSelectedUsers);
		util.clearSelection(users); // Normally we have to clear selected
									// properties, after operation finish
	}

	/**
	 * Remove all selected Locations from current message And clear selected
	 * propertied after this operation finish
	 * 
	 */

	public void removeSelectedLocationsFromCurrent() {
		List<Location> locations = this.getModel().getCurrent().getLocations();
		CollectionUtil<Location> util = new CollectionUtil<Location>();
		List<Location> unSelectedUsers = util.extractUnSelected(locations);
		this.getModelCurrent().setLocations(unSelectedUsers);
		util.clearSelection(locations); // Normally we have to clear selected
		// properties, after operation finish
	}

	/**
	 * Add all selected pictures to current message And clear selected
	 * propertied after this operation finish
	 * 
	 */
	public void addSelectedPicsToCurrent() {
		Message current = this.getModelCurrent();
		List<Picture> selectedPictures = picModel.getSelecteds();
		CollectionUtil<Picture> util = new CollectionUtil<Picture>();
		util.addAll(current.getPictures(), selectedPictures);
		util.clearSelection(selectedPictures); // Normally we have to clear
												// selected properties, after
												// operation finish
	}

	/**
	 * Remove all selected pictures from current message And clear selected
	 * propertied after this operation finish
	 * 
	 */
	public void removeSelectedPicsFromCurrent() {
		List<Picture> pics = this.getModel().getCurrent().getPictures();
		CollectionUtil<Picture> util = new CollectionUtil<Picture>();
		List<Picture> unSelectedPics = util.extractUnSelected(pics);
		this.getModelCurrent().setPictures(unSelectedPics);
		util.clearSelection(pics); // Normally we have to clear selected
									// properties, after operation finish
	}
}
